package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.Returnorderdetail;
import cn.itcast.erp.entity.Returnorders;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.OrderDetailReturnService;
import cn.itcast.erp.service.OrdersReturnService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采购退货Controller类
 *
 * @author 崔凯铭，罗宇航
 * @date 2019/6/12
 */

@RestController
@RequestMapping("/ordersreturn")
public class OrdersReturnController {

    @Resource
    private OrdersReturnService ordersReturnService;

    @Resource
    private OrderDetailReturnService orderDetailReturnService;

    @CheckPower("采购退货登记")
    @RequestMapping("/listByPage")
    public AjaxResult listByPage(Returnorders returnorders) {
        return ordersReturnService.findOrders(returnorders);
    }

    @CheckPower({"采购退货登记","销售退货登记"})
    @RequestMapping("/insert")
    public AjaxResult insertOrders(@RequestBody Returnorders returnorders, @SessionAttribute("emp") Emp emp) {
        AjaxResult ar = new AjaxResult();
        int i = this.ordersReturnService.insertOrders(returnorders,emp);
        if (i > 0) {
            ar.setMsg("添加成功");
            return ar;
        }
        ar.setMsg("添加失败");
        return ar;
    }

    @CheckPower("采购退货登记")
    @RequestMapping("/loadDetail")
    public List<Returnorderdetail> findOrderDetail(Returnorders returnorders) {
        return this.orderDetailReturnService.findOrderDetail(returnorders);
    }

    @CheckPower("采购退货登记")
    @RequestMapping("/find")
    public Returnorders findOrderByUuid(Returnorders returnorders) {
        return this.ordersReturnService.findOrderByUuid(returnorders);
    }

    @CheckPower({"采购退货登记","采购退货审核"})
    @RequestMapping("/check")
    public AjaxResult check(Returnorders returnorders, @SessionAttribute("emp") Emp emp) {
        AjaxResult ar = new AjaxResult();
        int i = this.ordersReturnService.checkOrder(returnorders, emp);
        if (i > 0) {
            ar.setMsg("审核成功");
            return ar;
        }
        ar.setMsg("审核失败");
        return ar;
    }

    @CheckPower({"采购退货登记","销售退货审核"})
    @RequestMapping("/start")
    public AjaxResult start(Returnorders returnorders, @SessionAttribute("emp") Emp emp) {
        AjaxResult ar = new AjaxResult();
        int i = this.ordersReturnService.startOrder(returnorders, emp);
        if (i > 0) {
            ar.setMsg("审核成功");
            return ar;
        }
        ar.setMsg("审核失败");
        return ar;
    }

    @CheckPower({"采购退货登记","销售退货入库"})
    @RequestMapping("/inStore")
    public AjaxResult inStore(Returnorderdetail returnorderdetail, @SessionAttribute("emp") Emp emp) {
        return this.ordersReturnService.inStore(returnorderdetail, emp);
    }

    @CheckPower({"采购退货登记","采购退货出库"})
    @RequestMapping("/outStore")
    public AjaxResult ourStore(Returnorderdetail returnorderdetail, @SessionAttribute("emp") Emp emp) {
        return this.ordersReturnService.outStore(returnorderdetail, emp);
    }

}
